package com.xianyun.book.gatewayimpl.handle.strategy;

import com.xianyun.book.common.core.event.DomainEventPublisher;
import com.xianyun.book.common.core.handle.SingleHandle;
import com.xianyun.book.common.core.handle.SingleInvoker;
import com.xianyun.book.common.core.handle.HandleParam;
import com.xianyun.book.common.util.StringUtils;
import com.xianyun.book.domain.book.Book;
import com.xianyun.book.domain.price.Currency;
import com.xianyun.book.dto.domainevent.BookQuerySuccessEvent;
import com.xianyun.book.gatewayimpl.database.DouBanLibraryMapper;
import com.xianyun.book.gatewayimpl.database.dataobject.DouBanLibraryDO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Collections;

/**
 * @author macos-zyj
 */
@Component
public class DouBanBookSingleHandle implements SingleHandle<Book,String> {

    @Resource
    private DouBanLibraryMapper douBanMapper;

    @Resource
    private DomainEventPublisher publisher;

    @Override
    public Book doQuery(HandleParam<String> handleParam, SingleInvoker<Book, String> next) {
        DouBanLibraryDO douBanLibrary = douBanMapper.queryBookInfoByIsbn(handleParam.getHandleData());
        if (checkDouBanLibraryEffective(douBanLibrary)){
            Book result = conventBook(douBanLibrary);
            BookQuerySuccessEvent queryEvent = new BookQuerySuccessEvent();
            BeanUtils.copyProperties(result,queryEvent);
            queryEvent.setBookStatus(result.getBookStatus().getStatusId());
            queryEvent.setTypeList(Collections.singletonList(douBanLibrary.getTag()));
            publisher.publish(queryEvent);
            return result;
        }else {
            return next.invoke(handleParam);
        }
    }


    private Book conventBook(DouBanLibraryDO book){
        Book resultBook = new Book();
        resultBook.setBookName(book.getTitle());
        resultBook.setIsbn(book.getIsbn());
        resultBook.setPress(book.getPublish());
        resultBook.setImgUrl(book.getImg());
        resultBook.setDesc(book.getBrief());
        resultBook.setPersonNumber(book.getLikePerson());
        if(StringUtils.isNotEmpty(book.getScore())){
            resultBook.setScore(Double.parseDouble(book.getScore()));
        }
        resultBook.preSale(new Currency(book.getPrice()));
        resultBook.setPersonNumber(book.getLikePerson());
        resultBook.setAuthor(book.getAuthor());
        resultBook.setPublishDate(book.getPublishTime());
        return resultBook;
    }


    private boolean checkDouBanLibraryEffective(DouBanLibraryDO douBanBook){
        if (null == douBanBook){
            return false;
        }
        if (StringUtils.isEmpty(douBanBook.getIsbn())){
            return false;
        }
        if (StringUtils.isEmpty(douBanBook.getImg())){
            return false;
        }
        if (StringUtils.isEmpty(douBanBook.getPrice())){
            return false;
        }
        return StringUtils.isNotEmpty(douBanBook.getBrief());
    }
}
